package com.jndv.phoneconfig.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.jndv.phoneconfig.PublicConstant;
import com.jndv.phoneconfig.R;
import com.jndv.phoneconfig.command.HotspotStateCommand;
import com.jndv.phoneconfig.command.MobileDataStateCommand;
import com.kyleduo.switchbutton.SwitchButton;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class MobileDataFragment extends BaseFragment {

    SwitchButton sbMobileData;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mobile_data;
    }

    @Override
    protected void initView(View view) {
        sbMobileData=view.findViewById(R.id.sb_mobile_data);
        sbMobileData.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sendMessage(PublicConstant.MOBILE_DATA,isChecked?"1":"0");
            }
        });

        SwitchButton sbAirPlaneMode=view.findViewById(R.id.sb_airplane_mode);
        sbAirPlaneMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sendMessage(PublicConstant.AIRPLANE_MODE,isChecked?"1":"0");
            }
        });

        if(isSocketConnect()){
            sendMessage(PublicConstant.CUR_MOBILE_DATA_STATE,"");
        }else {
            sbMobileData.setChecked(false);
            sbAirPlaneMode.setChecked(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCurHotspotStateEvent(MobileDataStateCommand event) {
        sbMobileData.setChecked(event.isOn);
    }
}
